package com.xiay.stringboot.server;

import com.xiay.stringboot.Exception.EmployeeException;
import com.xiay.stringboot.doman.Employee;
import com.xiay.stringboot.doman.Result;
import com.xiay.stringboot.enums.ResultEnum;
import com.xiay.stringboot.repository.EmployeeRepository;
import com.xiay.stringboot.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by H-209-FX-XY on 2018/4/23.
 */
@Service
public class EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;

    public void save(Employee employee){
        employeeRepository.save(employee);
    }

    public List<Employee> findAll(){
       return employeeRepository.findAll();
    }

    public List<Employee> findByUsername(String username) {
       return employeeRepository.findByUsername(username);
    }

    public Result findById(Integer id) throws Exception {
        Employee e = employeeRepository.findOne(id);
        if(e.getEname() == null ){
            throw new EmployeeException(ResultEnum.EMPLOYEE_NOT_ENAME);
        }
        return ResultUtil.success(e);
    }
}
